package matrici;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 
 * @author Luca
 */
public class MatriciProdottoRigaPerColonna extends JPanel implements ActionListener {

    static JFrame f;
    JComboBox rA, rB, cA, cB;
    JTextField[][] mA, mB, mC;
    Integer[] num = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    JLabel lA, lB;
    JButton ok, bU;
    JPanel initial, finale;
    Boolean flag = false;
    int nRA = 0, nRB = 0, nRC = 0, nCA = 0, nCB = 0, nCC = 0;

    /**
     * 
     */
    public MatriciProdottoRigaPerColonna() {
        initial = new JPanel();

        rA = new JComboBox(num);
        rB = new JComboBox(num);
        cA = new JComboBox(num);
        cB = new JComboBox(num);

        lA = new JLabel("  Matrice A   ");
        lB = new JLabel("  Matrice B   ");

        ok = new JButton("OK");

        this.add(initial);

        initial.setLayout(new GridLayout(3, 3));

        initial.add(lA);
        initial.add(rA);
        initial.add(cA);
        initial.add(lB);
        initial.add(rB);
        initial.add(cB);
        initial.add(ok);

        ok.addActionListener(this);
    }

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        MatriciProdottoRigaPerColonna m = new MatriciProdottoRigaPerColonna();
        f = new JFrame();
        f.setVisible(true);
        f.setTitle("Matrici: prodotto riga per colonna");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocation(200, 200);
        f.add(m);

        f.pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource().equals(ok)) {
            nRA = rA.getSelectedIndex() + 1;
            nRB = rB.getSelectedIndex() + 1;
            nCA = cA.getSelectedIndex() + 1;
            nCB = cB.getSelectedIndex() + 1;

            initial.setVisible(false);
            finale = new JPanel();
            JPanel pA = new JPanel();
            JPanel pB = new JPanel();
            JPanel pC = new JPanel();
            JPanel pS = new JPanel();
            JPanel pU = new JPanel();
            JLabel lS = new JLabel("  *  ");
            bU = new JButton("=");

            pU.setLayout(new BorderLayout());
            pS.setLayout(new BorderLayout());

            pU.add(bU, BorderLayout.CENTER);
            bU.addActionListener(this);
            pS.add(lS, BorderLayout.CENTER);

            finale.add(pA);
            finale.add(pS);
            finale.add(pB);
            finale.add(pU);
            finale.add(pC);

            pA.setLayout(new GridLayout(nRA, nCA));
            mA = new JTextField[nRA][nCA];
            for (int i = 0; i < nRA; i++) {
                for (int j = 0; j < nCA; j++) {
                    mA[i][j] = new JTextField("0");
                    mA[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    mA[i][j].setPreferredSize(new Dimension(20, 20));
                    mA[i][j].setAutoscrolls(true);
                    pA.add(mA[i][j]);
                }
            }

            pB.setLayout(new GridLayout(nRB, nCB));
            mB = new JTextField[nRB][nCB + 1];
            for (int i = 0; i < nRB; i++) {
                for (int j = 0; j < nCB; j++) {
                    mB[i][j] = new JTextField("1");
                    mB[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    mB[i][j].setPreferredSize(new Dimension(20, 20));
                    mB[i][j].setAutoscrolls(true);
                    pB.add(mB[i][j]);
                }
            }

            if (nCA == nRB) {
                nRC = nRA;
                nCC = nCB;
                pC.setLayout(new GridLayout(nRC, nCC));
                mC = new JTextField[nRC][nCC];
                for (int i = 0; i < nRC; i++) {
                    for (int j = 0; j < nCC; j++) {
                        mC[i][j] = new JTextField("0");
                        mC[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
                        mC[i][j].setPreferredSize(new Dimension(20, 20));
                        mC[i][j].setAutoscrolls(true);
                        pC.add(mC[i][j]);
                    }
                }
                flag = true;
            } else {
                pC.add(new JLabel("Non si può fare!"));
                flag = false;
            }

            this.add(finale);
            f.pack();
        }

        if (e.getSource().equals(bU) && flag) {
            int matriceA[][] = new int[nRA][nCA];
            int matriceB[][] = new int[nRB][nCB];
            int matriceC[][] = new int[nRC][nCC];

            for (int i = 0; i < nRA; i++) {
                for (int j = 0; j < nCA; j++) {
                    matriceA[i][j] = new Integer(mA[i][j].getText());
                }
            }

            for (int i = 0; i < nRB; i++) {
                for (int j = 0; j < nCB; j++) {
                    matriceB[i][j] = new Integer(mB[i][j].getText());
                }
            }

//            for (int i = 0; i < nRC; i++) {
//                for (int j = 0; j < nCC; j++) {
//                    matriceC[i][j] = prodottoScalare(matriceA, matriceB, i, j);
//                    mC[i][j].setText(new Integer(matriceC[i][j]).toString());
//                }
//            }
            
            matriceC=prodottoRighePerColonne(matriceA, matriceB);
            for (int i = 0; i < nRC; i++) {
                for (int j = 0; j < nCC; j++) {
                    for (int k = 0; k < nCA; k++) {
                        mC[i][j].setText(new Integer(matriceC[i][j]).toString());
                    }
                }
            }

        }
    }

    int prodottoScalare(int[][] A, int[][] B, int r, int c) {
        int temp = 0;
        for (int i = 0; i < nCA; i++) {
            temp += A[r][i] * B[i][c];
        }
        return temp;
    }

    double[][] prodottoRighePerColonne(double matriceA[][], double matriceB[][]) {
        if (matriceA != null & matriceB != null) {
            int numeroRigheA = matriceA.length;
            int numeroColonneB = matriceB[0].length;
            int numeroColonneA = matriceA.length;

            double[][] matriceC = new double[numeroRigheA][numeroColonneB];

            for (int i = 0; i < numeroRigheA; i++) {
                for (int j = 0; j < numeroColonneB; j++) {
                    for (int k = 0; k < numeroColonneA; k++) {
                        matriceC[i][j] += matriceA[i][k] * matriceB[k][j];
                    }
                }
            }
            return matriceC;
        } else {
            return null;
        }
    }

    int[][] prodottoRighePerColonne(int[][] matriceA, int[][] matriceB) {
        if (matriceA != null & matriceB != null) {
            int numeroRigheA = matriceA.length;
            int numeroColonneB = matriceB[0].length;
            int numeroColonneA = matriceA.length;

            int [][] matriceC = new int[numeroRigheA][numeroColonneB];

            for (int i = 0; i < numeroRigheA; i++) {
                for (int j = 0; j < numeroColonneB; j++) {
                    for (int k = 0; k < numeroColonneA; k++) {
                        matriceC[i][j] += matriceA[i][k] * matriceB[k][j];
                    }
                }
            }
            return matriceC;
        } else {
            return null;
        }
    }
}